/*
 * Copyright (c) Tan Menglong.
 * All rights reserved.
 * 
 * This file is part of the RabbitOS.  The RabbitOS is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the Tan Menglong at TanMenglong@gmail.com or +86-15972108715.
 * 
 * The RabbitOS is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the RabbitOS; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * Functions to operate descriptor tables.
 * 03-May-08
 */

#include"../include/rabbit/dt.h"

gdt_entry_t gdt_entries[5];
gdt_ptr_t gdt_ptr;

void set_gdt_gate(s32int num,
				  u32int base, u32int limit, u8int access, u8int gran)
{

	gdt_entries[num].base_low = (base & 0xFFFF);
	gdt_entries[num].base_middle = (base >> 16) & 0xFF;
	gdt_entries[num].base_high = (base >> 24) & 0xFF;

	gdt_entries[num].limit_low = (limit & 0xFFFF);
	gdt_entries[num].granularity = (limit >> 16) & 0x0F;

	gdt_entries[num].granularity |= gran & 0xF0;
	gdt_entries[num].access = access;
}

void init_gdt(void)
{
	gdt_ptr.limit = (sizeof(gdt_entry_t) * 5) - 1;
	gdt_ptr.base = (u32int) & gdt_entries;

	// Null segment
	set_gdt_gate(0, 0, 0, 0, 0);
	// Code segment
	set_gdt_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
	// Data segment
	set_gdt_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
	// User mode code segment
	set_gdt_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
	// User mode data segment
	set_gdt_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);

	flush_gdt((u32int) & gdt_ptr);
}

void init_idt(void)
{

}
